package br.com.sistemasaude.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.sistemasaude.bean.Atendimento;
import br.com.sistemasaude.bean.Medicamento;
import br.com.sistemasaude.bean.RelMedAtend;
import br.com.sistemasaude.util.JpaUtil;

public class RelMedAtendDAOImpl implements RelMedAtendDAO{
	
	private final GenericDAO<RelMedAtend> dao;	
	EntityManager em = new JpaUtil().getEntityManager();
	
	public RelMedAtendDAOImpl() {
		dao = new GenericDAO<RelMedAtend>(em, RelMedAtend.class);
		}
	
public void salvar(RelMedAtend relMedAtend) {
		
		try {

			em.getTransaction().begin();
			dao.salvar(relMedAtend);
			em.getTransaction().commit();

		} catch (Exception e) {
			
			em.getTransaction().rollback();
			e.printStackTrace();
			
		} finally {
			em.close();
		}
		
	}

public List<Medicamento> retornaMedicamentos(Atendimento atendimento){

	 List<Medicamento> medicamentos = new ArrayList<Medicamento>();
	Query query = em.createQuery("select r.tbMedicamento from RelMedAtend r where r.tbAtendimento = :pAtendimento ");
	query.setParameter("pAtendimento", atendimento);
	medicamentos = query.getResultList();
	return medicamentos;
	
}
	

}
